package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.scm.dto.EditInvDto;
import org.jeecg.modules.scm.entity.ScmFinaContract;
import org.jeecg.modules.scm.entity.ScmInvestor;
import org.jeecg.modules.scm.service.IScmInvestorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 资金方管理
 * @Author: zzq
 * @Date: 2021-10-21
 * @Version: V1.0
 */
@Api(tags="资金方管理")
@RestController
@RequestMapping("/scm/scmInvestor")
@Slf4j
public class ScmInvestorController extends JeecgController<ScmInvestor, IScmInvestorService> {
	@Autowired
	private IScmInvestorService scmInvestorService;
	
	/**
	 * 分页列表查询
	 *
	 * @param scmInvestor
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "scm_investor-分页列表查询")
	@ApiOperation(value = "分页列表查询", notes = "scm_investor-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ScmInvestor scmInvestor,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ScmInvestor> queryWrapper = QueryGenerator.initQueryWrapper(scmInvestor, req.getParameterMap());
		Page<ScmInvestor> page = new Page<ScmInvestor>(pageNo, pageSize);
		IPage<ScmInvestor> pageList = scmInvestorService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
     * 添加
     *
     * @param scmInvestor
     * @return
     */
    @AutoLog(value = "scm_investor-添加")
    @ApiOperation(value = "添加资金方信息", notes = "scm_investor-添加")
    @PostMapping(value = "/add")
    public Result<Integer> add(@RequestBody ScmInvestor scmInvestor) {
        scmInvestorService.save(scmInvestor);
        return Result.OK(scmInvestor.getInvestorId());
    }
	
	/**
	 *  编辑
	 *
	 * @param dto
	 * @return
	 */
	@AutoLog(value = "scm_investor-编辑")
	@ApiOperation(value = "编辑资金方信息", notes = "scm_investor-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody EditInvDto dto) {
		scmInvestorService.editById(dto);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "scm_investor-通过id删除")
	@ApiOperation(value = "通过id删除", notes = "scm_investor-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		scmInvestorService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "通过id查询")
	@ApiOperation(value = "scm_investor-通过id查询", notes = "scm_investor-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ScmInvestor scmInvestor = scmInvestorService.getById(id);
		if(scmInvestor==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(scmInvestor);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param scmInvestor
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScmInvestor scmInvestor) {
        return super.exportXls(request, scmInvestor, ScmInvestor.class, "scm_investor");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ScmInvestor.class);
    }



	 @ApiOperation(value = "模糊查询资金方名称列表", notes = "返回资金方名称对应的资金方ID列表")
     @GetMapping("/likeName")
     public Result<List<Map<String, Object>>> likeName(@RequestParam(value = "key") String key) {
         // 模糊查询客户名称
         List<Map<String, Object>> invNames = scmInvestorService.likeName(key);
		 return Result.OK(invNames);
	 }


	@ApiOperation(value = "查看资金方融融资情况", notes = "返回资金方关联的融资合同列表,按照“生效日期”最新排序展现")
    @GetMapping("/queryInvContractList")
	public Result<List<ScmFinaContract>> queryInvContractList(@RequestParam(value = "invId") Integer invId) {
		List<ScmFinaContract> contractList = scmInvestorService.queryInvContractList(invId);
		return Result.OK(contractList);
	}

	@ApiOperation(value = "上传附件", notes = "上传附件")
	@PostMapping("/uploadFile")
	public Result<List<String>> uploadFile(MultipartFile file, @RequestParam Integer invId) {
		String paths = scmInvestorService.uploadAccessory(file, invId);
		return Result.OK(Arrays.asList(paths.split(",")));
	}

}
